/*
 * Copyright (C) 2018 guomw.Inc. All rights reserved.
 * 项目名称： mysite
 * 文件名称： main.js
 * Date：18-9-19 下午4:53
 * Author: guomw
 */
$(function () {
    /**菜单模板加载*/
    var ul_template = $("#ul_template").html();
    var li_template = $("#li_template").html();
    var menuListProvider = {
        menuList: [],
        getChildMenu: function (itemParentCode) {
            var resultList = [];
            $.each(this.menuList, function (o, item) {
                if (item.itemParentCode === itemParentCode) {
                    resultList.push(item);
                }
            });
            return resultList;
        }
    };
    initData();

    /**
     * 加载数据
     */
    function initData() {
        hotUtil.loading.show();
        hotUtil.ajaxCall("/sys/ajax/leftlist", null, function (ret) {
            hotUtil.loading.close();
            if (ret.resultCode === 2000) {
                menuListProvider.menuList = ret.data;
                var appendHtml = ul_template.replace(/{li_template}/, outputChild(0)).replace(/{nav-child}/, "");
                $("#menus_items").html(appendHtml);
                $("#menus_items").metisMenu();
                refreshmenu();
            }
        });
    }

    /**
     * 递归输出菜单
     */
    function outputChild(itemParentCode) {
        var data = menuListProvider.getChildMenu(itemParentCode);
        var appendHtml = "";
        $.each(data, function (o, item) {
            if (!item.itemFunction) {
                var itemChildCount = item.itemChildCount;
                var tempUlHtml = ul_template;
                var tempHtml = li_template;
                tempHtml = tempHtml.replace(/{itemName}/gm, item.itemName);
                tempHtml = tempHtml.replace(/{iconClsDom}/gm, hotUtil.isNullOrEmpty(item.itemIconCls) ? "" : "<i class=\"fa fa-" + item.itemIconCls + "\"></i>");
                if (itemChildCount > 0) {
                    var _output = outputChild(item.itemCode);
                    tempHtml = tempHtml.replace(/{arrow}/gm, !hotUtil.isNullOrEmpty(_output) ? "<span class=\"fa arrow\"></span>" : "");
                    tempHtml = tempHtml.replace(/{href}/gm, !hotUtil.isNullOrEmpty(_output) ? "" : "href='" + item.itemPath + "'");
                    tempUlHtml = hotUtil.isNullOrEmpty(_output) ? "" : tempUlHtml.replace(/{li_template}/, _output).replace(/{nav-child}/, "nav-child");
                    tempHtml = tempHtml.replace(/{ul_template}/, tempUlHtml);
                }
                else {
                    tempHtml = tempHtml.replace(/{href}/gm, "href='" + item.itemPath + "'");
                    tempHtml = tempHtml.replace(/{arrow}/gm, "");
                    tempHtml = tempHtml.replace(/{ul_template}/, "");
                }
                appendHtml += tempHtml;
            }
        });
        return appendHtml;
    }
});



function newTab(url, name) {
    var index = false;
    $(".page-tabs-content .J_menuTab").each(function () {
        var dataId = $(this).attr("data-id");
        if (dataId == url) {
            index = true;
            $(this).click();
            var loado = layer.load();
            var jiframe = $('.J_iframe[data-id="' + url + '"]');
            jiframe.attr("src", jiframe.attr("src"));
            jiframe.load(function () {
                layer.close(loado);
            });
            return false;
        }
    });
    if (!index) {
        var s = '<a href="javascript:;" class="active J_menuTab" data-id="' + url + '">' + name + ' <i class="fa fa-times-circle"></i></a>';
        $(".J_menuTab").removeClass("active");
        var r = '<iframe class="J_iframe" width="100%" height="100%" src="' + url + '" frameborder="0" data-id="' + url + '" seamless></iframe>';
        $(".J_mainContent").find("iframe.J_iframe").hide().parents(".J_mainContent").append(r);
        var o = layer.load();
        $(".J_mainContent iframe:visible").load(function () {
            layer.close(o)
        }),
            $(".J_menuTabs .page-tabs-content").append(s);
    }
}
